/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

/**
 *
 * @author Raise
 */
import ui.AutoTestUI;
import network.CNN;
import date.MNISTDataLoader;
import date.ImageData;
import java.util.List;
import javax.swing.*;

public class Main {
   public static void main(String[] args) {
        try {
            System.out.println("开始加载MNIST数据...");
            
            // 加载MNIST数据
            MNISTDataLoader loader = new MNISTDataLoader();
            List<ImageData> trainingData = loader.loadTrainingData();
            List<ImageData> testData = loader.loadTestData();
            
            System.out.println("数据加载完成，\n训练集: " + trainingData.size() + " 个样本");
            System.out.println("测试集: " + testData.size() + " 个样本");
            
            // 使用CNN
            long seed = 33333L;
            CNN model = new CNN(0.001, seed); // 更小的学习率
            System.out.println("开始训练CNN神经网络...");
            model.train(trainingData, 10); // 更多训练轮次
            
            // 测试准确率
            System.out.println("开始测试...");
            int correct = 0;
            for (ImageData data : testData) {
                int predicted = model.predict(data.getPixels());
                if (predicted == data.getLabel()) {
                    correct++;
                }
            }
            double testAccuracy = (double)correct / testData.size();
            System.out.printf("最终测试集准确率: %.4f (%d/%d)\n", testAccuracy, correct, testData.size());
            
            // 启动自动测试界面
            System.out.println("启动自动测试界面...");
            SwingUtilities.invokeLater(() -> {
                new AutoTestUI(model);
            });
            
        } catch (Exception e) {
            System.out.println("错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
